import requests
import simplejson
from lxml import etree
from lxml.etree import HTMLParser
import io
import sys

#改变标准输出的默认编码
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
headers = {'User-Agent': DEFAULT_USER_AGENT}
def read_aritists_tag(url):
    start=0
    while True:
        turl = url % start
        response = requests.get(turl,headers=headers)
        print(response.status_code)
        html = etree.HTML(response.text,HTMLParser())
        style_list = html.xpath("//div[@class='indent']//span/a/text()")
        style_count_list  = html.xpath("//div[@class='indent']//span/text()")
        style_url_list = html.xpath("//div[@class='indent']//span/a/@href")
        if  "(1)\u3000\u3000\n        " in style_count_list:
            break
        start += 200
        style_count_list.pop(0)
        item_dict_list=[]
        for i in range(len(style_count_list)):
            style = style_list[i]
            style_count = style_count_list[i].strip().replace("(","").replace(")",""),
            for style_url in style_url_list:
                res = requests.get(style_url,headers=headers)
                style_detail_html = etree.HTML(res.text,HTMLParser())
                singers = style_detail_html.xpath("//div[@class='article']/div[@class='site_bar']/a/text()")[:10]
                singers_list = singers
                item_dict = {
                    'style':style,
                    'style_count':style_count,
                    'singers_list':singers_list
                }
                item_dict_list.append(item_dict)
        print(item_dict_list)

            


            



if __name__ == "__main__":
    url = "https://music.douban.com/artists/tag/?start=%s"
    read_aritists_tag(url)